import { getToken, setToken, getUser, setUser, removeInfo } from '@/utils/auth';
import loginApi from '@/api/login';
import { Message } from 'element-ui';
export default {
    state: {
        //一开始调用 作为初始值，解决刷新页面为null
        token: getToken(),
        userInfo: getUser(),
    },
    mutations: {
        //设置token 存入本地内存
        SET_TOKEN(state, token) {
            state.token = token;//存入state
            setToken(token);//存入本地内存
        },
        //设置用户信息  存入本地内存
        SET_USERINFO(state, userfrom) {
            state.userInfo = userfrom;
            setUser(userfrom);
        },
        //退出登录 用户信息 token清空
        LOGOUT(state) {
            removeInfo();
            state.token = null,
            state.userInfo = null;
        }
    },
    actions: {
        //发送登录请求，获取token
        Login({ commit }, { username, userpass }) {
            //返回一个promise对象
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await loginApi.loginIn(username, userpass);
                    //验证用户名、密码正确
                    if (p.data.flag) {
                        //将token 存入 本地
                        commit('SET_TOKEN', p.data.token);
                        //传入 用户信息
                        commit('SET_USERINFO', p.data.data);
                        resolve(p.data);
                    } else {
                        Message({
                            message: '用户名或密码错误',
                            type: "error"
                        })
                    }
                } catch (err) {
                    reject(err)
                }
            })
        },
        //验证tokn
        Checktoken({ commit,state }) {
            //返回一个promise对象
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await loginApi.checkToken(state.token);
                    let res;
                    //验证用户名、密码正确
                    if (p.data.flag) {
                        res = true;
                        resolve(res);
                    } else {
                        res = false;
                        resolve(res)
                        Message({
                            message: '用户名或密码错误',
                            type: "error"
                        })
                    }
                } catch (err) {
                    reject(err)
                }

            })
        },
        //退出登录
        logout({ commit, state }) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await loginApi.checkToken(state.token);
                    if (p.data.flag) {//退出成功
                        resolve(p.data);
                        commit('LOGOUT');
                    } else {//退出失败
                        Message({
                            message: '退出失败',
                            type: 'error'
                        })
                    }
                } catch (err) {
                    reject(err);
                }
            })
        }
    }
}